package com.liangsui.mss.service.impl;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liangsui.mss.constants.Constants;
import com.liangsui.mss.domain.DictData;
import com.liangsui.mss.dto.DictDataDto;
import com.liangsui.mss.mapper.DictDataMapper;
import com.liangsui.mss.service.DictDataService;
import com.liangsui.mss.vo.DataGridView;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
* @author zhang
* @description 针对表【sys_dict_data(字典数据表)】的数据库操作Service实现
* @createDate 2022-12-04 18:22:31
*/
@Slf4j
@Service
public class DictDataServiceImpl extends ServiceImpl<DictDataMapper, DictData> implements DictDataService {

    @Autowired
    private DictDataMapper dictDataMapper;

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public DataGridView listForPage(DictDataDto dictDataDto) {
        Page<DictData> dictDataPage = new Page<>(dictDataDto.getPageNum(), dictDataDto.getPageSize());
        QueryWrapper<DictData> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(dictDataDto.getDictType()),
                DictData.COL_DICT_TYPE, dictDataDto.getDictType());
        wrapper.like(StringUtils.isNotBlank(dictDataDto.getDictLabel()),
                DictData.COL_DICT_LABEL, dictDataDto.getDictLabel());
        wrapper.eq(StringUtils.isNotBlank(dictDataDto.getStatus()),
                DictData.COL_STATUS, dictDataDto.getStatus());
        dictDataMapper.selectPage(dictDataPage, wrapper);
        return new DataGridView(dictDataPage.getTotal(), dictDataPage.getRecords());
    }

    @Override
    public DictData selectDictDataById(Long dictId) {
        return dictDataMapper.selectById(dictId);
    }

    @Override
    public int insert(DictDataDto dictDataDto) {
        DictData dictData = new DictData();
        BeanUtils.copyProperties(dictDataDto, dictData);
        dictData.setCreateBy(dictDataDto.getSimpleUser().getUserName());
        dictData.setCreateTime(DateUtil.date());
        return dictDataMapper.insert(dictData);
    }

    @Override
    public int update(DictDataDto dictDataDto) {
        DictData dictData = new DictData();
        BeanUtils.copyProperties(dictDataDto, dictData);
        dictData.setUpdateBy(dictDataDto.getSimpleUser().getUserName());
        return dictDataMapper.updateById(dictData);
    }

    @Override
    public int deleteDictData(Long[] dictIds) {
        List<Long> ids = Arrays.asList(dictIds);
        if (null != ids && ids.size() > 0) {
            return dictDataMapper.deleteBatchIds(ids);
        } else {
            return -1;
        }
    }

    @Override
    public List<DictData> selectDictDataByDictType(String dictType) {
        String key = Constants.DICT_REDIS_PREFIX + dictType;
        ValueOperations<String, String> opsForValue = redisTemplate.opsForValue();
        String json = opsForValue.get(key);
        List<DictData> dictDatas = JSON.parseArray(json, DictData.class);
        if (null == dictDatas) {
            // redis当中没有缓存数据，则从数据库查询数据写到redis
            QueryWrapper<DictData> dictDataQueryWrapper = new QueryWrapper<>();
            dictDataQueryWrapper.eq(DictData.COL_DICT_TYPE, dictType);
            List<DictData> dictData = this.dictDataMapper.selectList(dictDataQueryWrapper);
            // 转换为json
            String dictData_type = JSON.toJSONString(dictData);
            ValueOperations<String, String> value = redisTemplate.opsForValue();
            value.set(Constants.DICT_REDIS_PREFIX + dictType, dictData_type);
            log.info("从数据库当中查询并且将字典数据存放到redis当中:{}");
        }
        log.info("根据类型字典查询字典数据成功:{}");
        return dictDatas;
    }
}




